﻿using NewRen.Helpers;
using NewRen.Log4net;
using NewRen.Models;
using NewRen.Models.DataModel;
using NewRen.Models.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NewRen.Controllers
{
    public class SearchController : BaseController
    {
        // GET: Search
        public ActionResult Index(string key, string sort)
        {
            try
            {
                int userCount = db.Users.Where(u => u.UserName.Contains(key) || u.RealName.Contains(key)).Count();
                int groupCount = db.UserGroups.Where(ug => ug.GroupName.Contains(key)).Count();
                var hot = new SearchHot();
                if ((string.IsNullOrEmpty(sort)) && !(string.IsNullOrEmpty(key)))
                {
                    hot = db.SearchHots.Where(h => h.Key == key).FirstOrDefault();
                    if (hot == null)
                    {
                        hot = new SearchHot();
                        hot.Key = key;
                        hot.Num = 1;
                        db.SearchHots.Add(hot);
                        db.SaveChanges();
                    }
                    else
                    {
                        hot.Num = hot.Num + 1;
                        db.SaveChanges();
                    }
                }
                ViewBag.SearchList = db.SearchHots.ToList();
                ViewBag.Users = db.Users.ToList();
                ViewBag.UserCount = userCount;
                ViewBag.GroupCount = groupCount;
                ViewBag.Key = key;
                ViewBag.HotSearch = db.SearchHots.OrderByDescending(s => s.Num).Take(5).ToList();
                return View();
            }
            catch (Exception ex)
            {
                log.Error(new LogContent("搜索出错", HttpHelper.GetIPAddress()), ex);
                return Redirect("/Shared/Info?msg=数据异常，请稍后重试");
            }
        }

        /// <summary>
        /// 牛人检索
        /// </summary>
        /// <param name="key"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public ActionResult SearchUsers(string key, int page, string sort)
        {
            AjaxModel ajaxModel = new AjaxModel();
            List<User> users = new List<Models.User>();
            List<vSearchModel> result = new List<vSearchModel>();
            try
            {
                var query = db.Users.Where(u => u.UserName.Contains(key) || u.RealName.Contains(key)).OrderByDescending(u => u.Priority);
                if (sort == "time")
                {
                    query = query.ThenByDescending(u => u.AddTime);
                }
                if (sort == "hot")
                {
                    query = query.ThenByDescending(u => u.Ups);
                }
                users = query.Skip(page * 10).Take(10).ToList();
                foreach (var item in users)
                {
                    result.Add(new vSearchModel(item));
                }
                ajaxModel.Data = result;
                ajaxModel.Statu = "ok";
            }
            catch (Exception ex)
            {
                ajaxModel.Msg = "数据异常";
                ajaxModel.Statu = "err";
                log.Error(new LogContent("牛人检索出错", HttpHelper.GetIPAddress()), ex);
            }
            return Json(ajaxModel);
        }

        /// <summary>
        /// 牛棚检索
        /// </summary>
        /// <param name="key"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public ActionResult SearchUserGroups(string key, string sort, int page)
        {
            AjaxModel ajaxModel = new AjaxModel();
            List<vSearchModel> result = new List<vSearchModel>();
            List<UserGroup> groups = new List<UserGroup>();
            try
            {
                var query = db.UserGroups.Where(ug => ug.GroupName.Contains(key)).OrderByDescending(ug => ug.Priority);
                if (sort == "time")
                {
                    query = query.ThenByDescending(ug => ug.AddTime);
                }
                if (sort == "hot")
                {
                    query = query.ThenByDescending(ug => ug.Ups);
                }
                groups = query.Skip(page * 10).Take(10).ToList();
                foreach (var item in groups)
                {
                    result.Add(new vSearchModel(item));
                }
                ajaxModel.Data = result;
                ajaxModel.Statu = "ok";
            }
            catch (Exception ex)
            {
                ajaxModel.Msg = "数据异常";
                ajaxModel.Statu = "err";
                log.Error(new LogContent("牛棚检索出错", HttpHelper.GetIPAddress()), ex);
            }
            return Json(ajaxModel);
        }
    }
}